﻿using UnityEngine;
using System.Collections;

public class PauseMenu : MonoBehaviour {

    private float savedTimeScale;
    private readonly string[] toolbarStrings = { "Grafik", "Audio", "Gameplay", "Steuerung" };
    private int toolbarInt;

    private MenuMaster menuMaster = new MenuMaster();
    private UserSettings userSettings = new UserSettings();
    
    public enum Page
    {
        None, Main, Options, 
    }

    private Page currentPage;

	void Start () {

        Time.timeScale = 1;
        //PauseGame();
	
	}

    bool IsGamePaused()
    {
        return (Time.timeScale == 0);
    }
	
	void Update () {

        if (Input.GetKeyDown("escape"))
        {
            switch (currentPage)
            {
                case Page.None:
                    PauseGame();
                    break;
                case Page.Main:
                    break;
                case Page.Options:
                    currentPage = Page.Main;
                    break;
                default:
                    currentPage = Page.Main;
                    break;
            }
        }
	
	}

    void MainPauseMenu()
    {
        menuMaster.BeginGUIArea(200, 200);
        
        if(GUILayout.Button("fortsetzen"))
            UnPauseGame();
        if(GUILayout.Button("Optionen"))
            currentPage = Page.Options;
        if(GUILayout.Button("Beenden"))
            Application.LoadLevel(1);

        menuMaster.EndGUIArea();
    }

    void PauseGame()
    {
        savedTimeScale = Time.timeScale;
        Time.timeScale = 0;
        AudioListener.pause = true;
        currentPage = Page.Main;
    }

    void UnPauseGame()
    {
        Time.timeScale = savedTimeScale;
        AudioListener.pause = false;
        currentPage = Page.None;
    }

    void OnGUI()
    {
        if (IsGamePaused())
        {
            switch (currentPage)
            {
                case Page.None:
                    break;
                case Page.Main:
                    MainPauseMenu();
                    break;
                case Page.Options:
                    Options();
                    break;
                default:
                    break;
            }
        }
    }

    void Options()
    {
        menuMaster.BeginGUIArea(500, 500);

        this.toolbarInt = GUILayout.Toolbar(this.toolbarInt, this.toolbarStrings);

        switch (toolbarInt)
        {
            case 0:
                menuMaster.GraphicSettings();
                break;
            case 1:
                menuMaster.AudioSettings();
                break;
            case 3:
                menuMaster.InputSettings();
                break;
        }

        if (GUI.Button(new Rect(10, 400, 100, 30), "Zurück"))
        {
            currentPage = Page.Main;
        }


        if (GUI.Button(new Rect(400, 400, 100, 30), "Übernehmen"))
        {
            userSettings.saveSettings();
        }

        menuMaster.EndGUIArea();
    }
}
